package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.TbRoomInfo;
import com.ruoyi.system.mapper.TbRoomMapper;
import com.ruoyi.system.domain.TbRoom;
import com.ruoyi.system.service.ITbRoomService;

/**
 * 房间管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-11
 */
@Service
public class TbRoomServiceImpl implements ITbRoomService 
{
    @Autowired
    private TbRoomMapper tbRoomMapper;

    /**
     * 查询房间管理
     * 
     * @param id 房间管理主键
     * @return 房间管理
     */
    @Override
    public TbRoom selectTbRoomById(Long id)
    {
        return tbRoomMapper.selectTbRoomById(id);
    }

    /**
     * 查询房间管理列表
     * 
     * @param tbRoom 房间管理
     * @return 房间管理
     */
    @Override
    public List<TbRoom> selectTbRoomList(TbRoom tbRoom)
    {
        return tbRoomMapper.selectTbRoomList(tbRoom);
    }

    /**
     * 新增房间管理
     * 
     * @param tbRoom 房间管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbRoom(TbRoom tbRoom)
    {
        tbRoom.setCreateTime(DateUtils.getNowDate());
        // 只保留images字段的第一个url
        if (tbRoom.getImages() != null && tbRoom.getImages().contains(",")) {
            tbRoom.setImages(tbRoom.getImages().split(",")[0]);
        }
        int rows = tbRoomMapper.insertTbRoom(tbRoom);
        insertTbRoomInfo(tbRoom);
        return rows;
    }

    /**
     * 修改房间管理
     * 
     * @param tbRoom 房间管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbRoom(TbRoom tbRoom)
    {
        tbRoom.setUpdateTime(DateUtils.getNowDate());
        // 只保留images字段的第一个url
        if (tbRoom.getImages() != null && tbRoom.getImages().contains(",")) {
            tbRoom.setImages(tbRoom.getImages().split(",")[0]);
        }
        tbRoomMapper.deleteTbRoomInfoByRoomId(tbRoom.getId());
        insertTbRoomInfo(tbRoom);
        return tbRoomMapper.updateTbRoom(tbRoom);
    }

    /**
     * 批量删除房间管理
     * 
     * @param ids 需要删除的房间管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbRoomByIds(Long[] ids)
    {
        tbRoomMapper.deleteTbRoomInfoByRoomIds(ids);
        return tbRoomMapper.deleteTbRoomByIds(ids);
    }

    /**
     * 删除房间管理信息
     * 
     * @param id 房间管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbRoomById(Long id)
    {
        tbRoomMapper.deleteTbRoomInfoByRoomId(id);
        return tbRoomMapper.deleteTbRoomById(id);
    }

    /**
     * 新增房间详情信息
     * 
     * @param tbRoom 房间管理对象
     */
    public void insertTbRoomInfo(TbRoom tbRoom)
    {
        List<TbRoomInfo> tbRoomInfoList = tbRoom.getTbRoomInfoList();
        Long id = tbRoom.getId();
        if (StringUtils.isNotNull(tbRoomInfoList))
        {
            List<TbRoomInfo> list = new ArrayList<TbRoomInfo>();
            for (TbRoomInfo tbRoomInfo : tbRoomInfoList)
            {
                tbRoomInfo.setRoomId(id);
                list.add(tbRoomInfo);
            }
            if (list.size() > 0)
            {
                tbRoomMapper.batchTbRoomInfo(list);
            }
        }
    }
}
